// pages/index/free/voluroom.js
//import radom from '../../../utils/radom.js'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    modelbar: true,
    //animationData: {},
    city: '',
    cityid: '',
    proviceid: '',
    landing_page:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
   
    // setTimeout(function() {
    //   var random2 = Math.round(Math.random() * 9)
    //   that.setData({
    //     num1: random2,
    //     pushmeage: true
    //   })
    // }, 30000)
    that.viewhouse()
    that.getbidding()
    that.setData({
      polist: '22小时前 用户134****5678申请验房'
    })
  },
  //加载来源页面
  viewhouse: function(e) {
    var that =this
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=view_house_inspection'),
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
        //'content-type': 'application/json;charset=utf-8'
      },
      success: function(res) {
        console.log(res.data)
        var landing_page = res.data.datas.landing_page
        var bind_count = res.data.datas.bid_count
        var s1 = bind_count.substring(0, 1);//取前半部分
        var s2 = bind_count.substring(1, 2);
        var s3 = bind_count.substring(2,3);
        var bids = bind_count.replace(/\b(0+)/gi, "")
        that.setData({
          landing_page:landing_page,
          num1:s1,
          num2:s2,
          num3:s3,
          count: bids
        })
      },
      fail:function(){
        console.log('请求错误')
      }
    })
  },
  //消息弹出框请求
  getbidding: function() {
    var that = this
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=get_bidding_list'),
      data: {
        type: 5
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        console.log(res.data)
        var polist = res.data.datas
        var i = 0; //在外面定义一个变量作为判断的标准
        var timer = setInterval(function() {
          //console.log(polist[i]);
          that.setData({
            polist: polist[i]
          })
          i++;
          if (i > polist.length - 1) { //因为i++的原因，所以当i的值大于数组的长度-1的时候，清除定时器
            clearInterval(timer);
          }
        }, 8000);
      }
    })
  },
  settime:function(){
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      }) 
    },2000)
  },
  //form表单跳转提交
  formSubmit: function(e) {
    var that = this
    var cityid = that.data.cityid
    var proviceid = that.data.proviceid
    var city = that.data.city
    var aleaname = e.detail.value.rqname
    var area = e.detail.value.area
    var tele = e.detail.value.tele
    var landing_page = that.data.landing_page
    var id = 2
    var title = '申请资料提交成功'
    var tsmall = '恭喜您，申请资料成功，我们将尽快与您联系!'
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (city == '') {
      that.setData({
        alertmess:true,
        message: '请选择城市！'
      })
      that.settime()
      return false;
    } else if (aleaname == '') {
      that.setData({
        alertmess: true,
        message:'请填写小区名称！'
      })
      that.settime()
    } else if (area == '') {
      that.setData({
        alertmess: true,
        message: '请填写房屋面积！'
      })
      that.settime()
    } else if (tele == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
      return false;
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误,请重新输入！'
      })
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_house_inspection'),
        data: {
          phone: tele,
          address: aleaname,
          acreage: area,
          province: proviceid,
          city: cityid,
          landing_page: landing_page
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          var code = res.data.code
          var message =res.data.error
          console.log(res.data)
          if(code==200){
            wx: wx.navigateTo({
              url: 'applysuccess?data=' + [id, title, tsmall],
            })
          } else if (code == 400) {
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }  
        }
      })
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease-in-out",
      delay: 0
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var animations = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease-in-out',

    })
    //this.animation = animations
    this.setData({
      animationData: animations.export()
    })
    var n = 0;
    var m = true;
    //连续动画需要添加定时器,所传参数每次+1就行
    setInterval(function() {
      n = n + 1;
      // console.log(n);
      if (m) {
        animations.translateY(-100).step()
        m = !m;
        this.setData({
          modelbar: true
        })
      } else if (m) {
        animations.translateY(-120).step()
        m = !m;
        this.setData({
          modelbar: true
        })
      } else {
        animations.translateY(120).step()
        m = !m;
        this.setData({
          modelbar: false
        })
      }
      this.setData({
        animationData: animations.export()
      })
    }.bind(this), 10000)

    var animatione = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
      transformOrigin: '50% 50%',
    })
    this.animation = animatione
    var next = true;
    //连续动画关键步骤
    setInterval(function() {
      if (next) {
        this.animation.scale(0.95).step()
        next = !next;
      } else {
        this.animation.scale(1).step()
        next = !next;
      }
      this.setData({
        animationDatas: animatione.export()
      })
    }.bind(this), 500)

  },
  //城市选择
  choosecity: function() {
    wx.navigateTo({
      url: '../public/custom/cucity',
    })
  },
  skipTop: function(e) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  }
})